package com.cby.library.base.web;

import android.net.Uri;
import android.webkit.WebResourceResponse;

import com.cby.code.util.CbyLogUtils;
import com.cby.library.base.ssl.HttpsSSLSocketFactory;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;

/**
 * @author ChenBaoyang
 * @description: WebUtils
 * @date 2021/12/1 09:53
 */
public class WebUtils {

    private static final String PREFIX_HTTPS = "https";
    private static final String SEMICOLON = ";";

    public static SSLContext getSSLContext() {
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("SSL");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sslContext;

    }

    public static WebResourceResponse processRequest(Uri uri) {
        CbyLogUtils.flog("SSL_PINNING_WEBVIEWS", "GET: " + uri.toString());

        try {
            // 定义获取的资源流，资源类型，资源编码
            InputStream is;
            String contentType;
            String encoding;
            // 设置一个url链接
            URL url = new URL(uri.toString());
            // 如果是http请求，这里如果加载的是http请求，则也要建立HttpURLConnection而不是默认加载。
            if (!uri.toString().toLowerCase(Locale.ROOT).startsWith(PREFIX_HTTPS)) {
                // https请求
                HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                urlConnection.setSSLSocketFactory(HttpsSSLSocketFactory.factory());
                is = urlConnection.getInputStream();
                contentType = urlConnection.getContentType();
                encoding = urlConnection.getContentEncoding();
            } else {
                // http请求
                HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
                is = httpUrlConnection.getInputStream();
                contentType = httpUrlConnection.getContentType();
                encoding = httpUrlConnection.getContentEncoding();
            }

            // 如果信息头中的资源类型不为null,则继续
            if (contentType != null) {

                String mimeType = contentType;

                // 解析出mimeType
                if (contentType.contains(SEMICOLON)) {
                    mimeType = contentType.split(SEMICOLON)[0].trim();
                }

                CbyLogUtils.flog("SSL_PINNING_WEBVIEWS", "Mime: " + mimeType);

                // 返回设置重新设置过的请求
                return new WebResourceResponse(mimeType, encoding, is);
            }

        } catch (SSLHandshakeException e) {
            e.printStackTrace();
            CbyLogUtils.flog("SSL_PINNING_WEBVIEWS", e.getLocalizedMessage());
        } catch (Exception e) {
            e.printStackTrace();
            CbyLogUtils.flog("SSL_PINNING_WEBVIEWS", e.getLocalizedMessage());
        }

        // 返回一个空的请求
        return new WebResourceResponse(null, null, null);
    }

    public static boolean isCause(Class<? extends Throwable> expected, Throwable exc) {
        return expected.isInstance(exc) || (exc != null && isCause(expected, exc.getCause()));
    }
}
